﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MoPhongThuatToan
{
    class MaTran
    {
        //private string s;
        
        public static int spt;
        public static int[,] matran;
        public static int[,] a;
        public struct Canh
        {
            public int d;
            public int c;
            public int TL;
        }
        public static List<Canh> DSCanh;
        public static List<int> DSDinh;

        public static int sodinh;
        public static List<Canh> DSCanhInput;

        public static void Docfile(string filename)
        {

            FileStream fp = new FileStream(filename, FileMode.Open);
            StreamReader SR = new StreamReader(fp);
            spt = Convert.ToInt32(SR.ReadLine());
            a = new int[spt, spt];
            matran = new int[spt, spt];
            for (int i = 0; i < spt; i++)
            {
                string Line = SR.ReadLine();
                int[] G = new int[spt];
                G=XuLyDong(Line);
                for (int j = 0; j < spt; j++)
                {
                    a[i, j] = G[j];
                    matran[i, j] = G[j];
                }
            }

            SR.Close();
            fp.Close();
        }

        public static void Docfile2(string filename)
        {
            FileStream fp = new FileStream(filename, FileMode.Open);
            StreamReader SR = new StreamReader(fp);
            sodinh = Convert.ToInt32(SR.ReadLine());
            DSCanhInput = new List<Canh>();
            for (int i = 0;i>-1 ; i++)
            {
                string Line = SR.ReadLine();
                if (Line == null)
                    break;
                int[] G = new int[3];
                G = XuLyDong(Line); 
                Canh canh = new Canh();
                canh.d = G[0];
                canh.c = G[1];
                canh.TL = G[2];
                DSCanhInput.Add(canh);
            }
            SR.Close();
            fp.Close();
            ChuyenDoi();
        }
        public static void ChuyenDoi()
        {

            a = new int[sodinh, sodinh];
            spt = sodinh;
            for (int i = 0; i < DSCanhInput.Count; i++)
            {
                a[DSCanhInput[i].d, DSCanhInput[i].c] = DSCanhInput[i].TL;

            }
        }
        public static int[] XuLyDong(string line)
        {
            string[] parts = line.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
            return Array.ConvertAll<string, int>(parts, int.Parse); 
            
        }

        public static void Ghifile(string filename)
        {
            FileStream fp = new FileStream(filename, FileMode.Create);
            StreamWriter SW = new StreamWriter(fp);
            SW.WriteLine(spt);
            for (int i = 0; i < spt; i++)
            {
                for (int j = 0; j < spt; j++)
                {
                    SW.Write(a[i, j]);
                    SW.Write("\t");
                }
                SW.WriteLine();
            }

            SW.Close();
            fp.Close();
        }
        public static void PhatSinh(int sd,int sc)
        {

            Random ran = new Random();
            spt = sd;
            a = new int[sd, sd];
            while (sc > 0)
            {
                int d = ran.Next(0,sd);
                int c = ran.Next(0,sd);
                while (d == c || a[d, c] != 0)
                {
                    d = ran.Next(0, sd);
                    c = ran.Next(0, sd);
                }
                a[d, c] = ran.Next(1,25);
                a[c, d] = a[d, c];
                sc--;
            }

        }
        public static void ListCanh()
        {
            DSCanh = new List<Canh>();
            Canh canh = new Canh();
            for (int i = 0; i < spt; i++)
                for (int j = i + 1; j < spt; j++)
                {
                    if (a[i, j] != 0)
                    {
                        canh.d = i;
                        canh.c = j;
                        canh.TL = a[i, j];
                        DSCanh.Add(canh);
                    }
                }
        }
        public static void ListDinh()
        {
            DSDinh = new List<int>();
            for (int i = 0; i < spt; i++)
            {
                DSDinh.Add(i);
            }
        }

    }
}
